package com.zch.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;

import com.zch.dao.model.ServiceType;
import com.zch.dao.model.ServiceTypeExample;
import com.zch.dao.model.TradeExample;
import com.zch.daoEx.model.ServiceTypeEx;
import com.zch.daoEx.model.TradeEx;
import com.zch.service.ServiceTypeService;

@RequestMapping("serviceType")
@Controller
@LoginFilter
public class ServiceTypeController extends CoreController{

	@Autowired
	private ServiceTypeService serviceTypeService;

	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
		PaginationSupport.byPage(page, rows);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		List<ServiceTypeEx> list =serviceTypeService.selectList(map);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(ServiceType serviceType){
		if (StringUtils.isBlank(serviceType.getId())){
		    serviceType.setCreateTime(new Date());
		    serviceType.setModifyTime(new Date());
			serviceTypeService.insert(serviceType);
		}else{
			serviceTypeService.updateByPrimaryKeySelective(serviceType);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		ServiceType serviceType = serviceTypeService.selectByPrimaryKey(id);
		return JSONUtils.serialize(serviceType);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			serviceTypeService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/myList")
    @ResponseBody
    public String myList(){
        Map<String, Object> map = new HashMap<String, Object>();
        List<ServiceTypeEx> list = serviceTypeService.selectList(map);
        return JSONUtils.serialize(list);
    }
	
	@RequestMapping("/isNoExist")
    @ResponseBody
    public String isNoExist(String value){
        ServiceTypeExample example = new ServiceTypeExample();
        example.createCriteria().andServiceTypeIdEqualTo(Integer.parseInt(value));
        int cnt = serviceTypeService.countByExample(example);
        if(cnt>0){
            return JSONUtils.serialize(new Ret(1)); 
        }        
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/isNameExist")
    @ResponseBody
    public String isNameExist(String value){
        ServiceTypeExample example = new ServiceTypeExample();
        example.createCriteria().andServiceTypeNameEqualTo(value);
        int cnt = serviceTypeService.countByExample(example);
        if(cnt>0){
            return JSONUtils.serialize(new Ret(1)); 
        }        
        return JSONUtils.serialize(new Ret(0));
    }
}